package model

import (
	"time"
	"gorm.io/gorm"
)

// Role 角色模型
type Role struct {
	ID          uint           `json:"id" gorm:"primarykey"`
	Name        string         `json:"name" gorm:"size:50;not null" binding:"required"`
	Description string         `json:"description" gorm:"size:200"`
	Status      int8           `json:"status" gorm:"default:1"` // 1:启用 0:禁用
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `json:"-" gorm:"index"`
	
	// 关联关系
	Menus       []Menu       `json:"menus" gorm:"many2many:role_menus;"`
	Permissions []Permission `json:"permissions" gorm:"many2many:role_permissions;"`
}

// RoleMenu 角色菜单关联表
type RoleMenu struct {
	RoleID uint `json:"role_id" gorm:"primaryKey"`
	MenuID uint `json:"menu_id" gorm:"primaryKey"`
}

// RolePermission 角色权限关联表
type RolePermission struct {
	RoleID       uint `json:"role_id" gorm:"primaryKey"`
	PermissionID uint `json:"permission_id" gorm:"primaryKey"`
}

// TableName 指定表名
func (Role) TableName() string {
	return "roles"
}

func (RoleMenu) TableName() string {
	return "role_menus"
}

func (RolePermission) TableName() string {
	return "role_permissions"
}

// RoleCreateRequest 创建角色请求
type RoleCreateRequest struct {
	Name        string `json:"name" binding:"required,min=2,max=50"`
	Description string `json:"description" binding:"max=200"`
	Status      int8   `json:"status"`
	MenuIDs     []uint `json:"menu_ids"`
	PermissionIDs []uint `json:"permission_ids"`
}

// RoleUpdateRequest 更新角色请求
type RoleUpdateRequest struct {
	Name        string `json:"name" binding:"required,min=2,max=50"`
	Description string `json:"description" binding:"max=200"`
	Status      int8   `json:"status"`
}

// RoleMenusUpdateRequest 更新角色菜单请求
type RoleMenusUpdateRequest struct {
	MenuIDs []uint `json:"menu_ids" binding:"required"`
}

// RolePermissionsUpdateRequest 更新角色权限请求
type RolePermissionsUpdateRequest struct {
	PermissionIDs []uint `json:"permission_ids" binding:"required"`
}

// RoleResponse 角色响应
type RoleResponse struct {
	ID          uint      `json:"id"`
	Name        string    `json:"name"`
	Description string    `json:"description"`
	Status      int8      `json:"status"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}
